﻿using Http_Get_Post_Test.Medol;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Http_Get_Post_Test
{
    public partial class HttpTestForm : Form
    {
        public HttpTestForm()
        {
            InitializeComponent();
        }


        private void Get_Click(object sender, EventArgs e)
        {
            int param1 = Convert.ToInt32(Get1.Text);
            string param2 = Get2.Text;
            var res = GetMethod(param1, param2);
            MessageBox.Show(res);
        }

        /// <summary>
        /// Get方式调用函数
        /// </summary>
        /// <param name="param1"></param>
        /// <param name="param2"></param>
        /// <returns></returns>
        private string GetMethod(int param1, string param2)
        {
            //根据用户请求的Url构造请求地址
            string url = $"http://localhost:59343/api/Test/Hello_Get?param1={param1}&param2={param2}";
            //创建web访问对象
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
            //获取网站访问后响应内容
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            //通过响应内容流创建StreamReader对象
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            //返回文本
            string returnXml = reader.ReadToEnd();
            //关闭流
            myResponse.Close();
            //返回
            return returnXml;
        }

        private void Post_Click(object sender, EventArgs e)
        {
            int param1 = Convert.ToInt32(Post1.Text);
            string param2 = Post2.Text;
            var res = PostMethod(param1, param2);
            MessageBox.Show(res);
        }

        /// <summary>
        /// POST方式调用
        /// </summary>
        /// <param name="param1"></param>
        /// <param name="param2"></param>
        /// <returns></returns>
        private string PostMethod(int param1, string param2)
        {
            string url = $"http://localhost:59343/api/Test/Hello_Post";
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            #region 添加POST参数
            //参数构造
            StringBuilder builder = new StringBuilder();
            builder.Append($"shuzi={param1}");
            builder.Append($"&str={param2}");

            byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
            myRequest.ContentLength = data.Length;
            using (Stream reqStream = myRequest.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            Stream stream = myResponse.GetResponseStream();
            //获取响应内容
            string result = "";
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }

        private void JsonConvert_Click(object sender, EventArgs e)
        {
            //根据用户请求的Url构造请求地址
            string url = $"http://localhost:59532/api/zjsystem/GetWpts?argID=8b8beb00-c41f-4b18-b070-5e6ae7dfab6c";
            //创建web访问对象
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
            //获取网站访问后响应内容j
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            //通过响应内容流创建StreamReader对象
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            //返回文本
            string returnJson = reader.ReadToEnd();

            //json数据转换测试
            JArray obj = JArray.Parse(returnJson);//针对数组类型json
            //JObject.Parse(returnJson)//针对对象类json
            var data = obj.ToObject<List<TestMedol>>();

            //关闭流
            myResponse.Close();
        }
    }
}
